package edu.sctu.xialei.june.day20150628two_test.IO;

import edu.sctu.xialei.june.day20150628two_test.json.AppBean;

import java.io.*;

/**
 * Created by Administrator on 2015/6/28.
 */
public class IOStreamUtil {

    public void saveFile(String file, String path) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            fileOutputStream.write(file.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getOutFile(String path) {

        String value = "";

        try {
            FileInputStream fileInputStream = new FileInputStream(path);

            int count;
            byte[] buffer = new byte[1024];
            while ( (count = fileInputStream.read(buffer) ) != -1) {
                value = value + new String(buffer,0,count,"utf-8");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return value;
    }

    public void saveFileBySER(AppBean appBean, String path) {

        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            objectOutputStream.writeObject(appBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public AppBean getOutFileBySER(String path) {

        AppBean appBean = null;

        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(path));
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            appBean = (AppBean) objectInputStream.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return appBean;
    }
}